/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ActividadListar.java
 *
 * Created on 08/06/2011, 03:15:21 PM
 */
package org.gingresos.vehiculos;

import org.gingresos.regimenimpositivo.*;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import org.gingresosapi.beans.*;
import org.gingresosapi.controller.*;
import org.gingresosapi.generales.Mensajeria;
import org.gingresos.start.MySesion;
import org.gingresosapi.reportes.clases.MyCertificacionVehicular;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author avbravo
 */
public class VehiculosReeimprimirCertificacion extends javax.swing.JInternalFrame {

    DefaultTableModel modelo;
    int fila = -1;
    Comercios comercios1 = new Comercios();
    ComerciosController comerciosController1 = new ComerciosController();
    Contribuyentes contribuyentes1 = new Contribuyentes();
    ContribuyentesController contribuyentesController1 = new ContribuyentesController();
    MyCertificacionVehicular myCertificacionVehicular = new MyCertificacionVehicular();
    Vehiculos vehiculos1 = new Vehiculos();
    VehiculosController vehiculosController1 = new VehiculosController();
    String nota = "Este vehiculo ";

    /**
     * Creates new form ActividadListar
     */
    public VehiculosReeimprimirCertificacion() {
        initComponents();
        try {
            AutoCompleteDecorator.decorate(jXComboBoxVehiculos);
            jXComboBoxVehiculos.removeAllItems();
            String sql = "idmunicipio='" + MySesion.getIdmunicipio() + "' and activo='si'";
            if (vehiculosController1.query(sql)) {
                for (Vehiculos v : vehiculosController1.getVehiculoslist()) {
                    jXComboBoxVehiculos.addItem(v.getPlaca());
                }
            } else {
                Mensajeria.Mensaje("No existen registros " + vehiculosController1.getMensaje());
            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "init()");
        }
        jButtonImprimir.setEnabled(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        abstractAreaEffect1 = new org.jdesktop.swingx.painter.effects.AbstractAreaEffect();
        jPanel1 = new javax.swing.JPanel();
        jButtonCerrar = new javax.swing.JButton();
        jButtonImprimir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jXComboBoxVehiculos = new org.jdesktop.swingx.JXComboBox();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Reeimpresion Certificacion  Vehicular");

        jButtonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/return.png"))); // NOI18N
        jButtonCerrar.setToolTipText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        jButtonImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/gingresos/resources/gif/print.png"))); // NOI18N
        jButtonImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirActionPerformed(evt);
            }
        });

        jLabel1.setText("Placa");

        jXComboBoxVehiculos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXComboBoxVehiculosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jLabel1)
                        .addGap(37, 37, 37)
                        .addComponent(jXComboBoxVehiculos, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(254, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jXComboBoxVehiculos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(87, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        getAccessibleContext().setAccessibleName("Reeimpresion Certificacion  Vehicula");

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
// TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jButtonCerrarActionPerformed

private void jButtonImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirActionPerformed
// TODO add your handling code here:
    try {

        // map.put("P_CONTRIBUYENTE",)
         /*
         * Busco el dueño
         */
        List<MyCertificacionVehicular> myCertificacionVehicularsList = new ArrayList<MyCertificacionVehicular>();
        myCertificacionVehicular.setVehiculos(vehiculos1);
        
       if(vehiculos1.getTipoid().equals("contribuyentes")){
                contribuyentes1.setIdmunicipio(MySesion.getIdmunicipio());
                contribuyentes1.setCedula(vehiculos1.getId());
                Contribuyentes c = contribuyentesController1.findByPrimaryKey(contribuyentes1);
                if (c == null) {
                    Mensajeria.Mensaje("No exisgte un contribuyente con la cedula " + contribuyentes1.getCedula());
                } else {
                    myCertificacionVehicular.setContribuyente(c.getContribuyente());
                    myCertificacionVehicular.setCedula(c.getCedula());
                    myCertificacionVehicular.setResidenteen(c.getDomicilio());
                    myCertificacionVehicular.setTelefono(c.getTelefono());
                }
              }
           if(vehiculos1.getTipoid().equals("comercios")){
                comercios1.setIdmunicipio(MySesion.getIdmunicipio());
                comercios1.setIdcomercio(vehiculos1.getId());
                Comercios com = comerciosController1.findByPrimaryKey(comercios1);
                if (com == null) {
                    Mensajeria.Mensaje("No exisgte un comercio con el codigo " + contribuyentes1.getCedula());
                } else {
                    myCertificacionVehicular.setContribuyente(com.getComercio());
                    myCertificacionVehicular.setCedula(com.getRuc());
                    myCertificacionVehicular.setResidenteen(com.getDireccion());
                    myCertificacionVehicular.setTelefono(com.getTelefono());
                }
     
        }//switch
        //Buscar las hipotecas, secuestros

        String hipoteca = "";
        String secuestro = "";
        if (vehiculos1.getHipotecado().equals("si")) {
            hipoteca = "registra hipoteca y";
        } else {
            hipoteca = "no registra hipoteca y";
        }
        if (vehiculos1.getSecuestrado().equals("si")) {
            secuestro = "registra caso judicial pendiente";

        } else {
            secuestro = "no registra caso judicial pendiente";
        }

        if (vehiculos1.getFideicomiso().equals("si")) {
            secuestro = "registra fideicomiso pendiente";

        } else {
            secuestro = "no registra fideicomiso pendiente";
        }
        nota += " " + hipoteca + " " + secuestro;

        HashMap map = new HashMap();
        map.put("P_MUNICIPIO", MySesion.getMunicipio());
        map.put("P_NOMBREUSUARIO", MySesion.getNombreusuario());
        map.put("P_NOTA", this.nota);
        myCertificacionVehicularsList.add(myCertificacionVehicular);

        Imprimir(myCertificacionVehicularsList, map);



    } catch (Exception ex) {
        Mensajeria.MensajeError(ex, "Imprimir()");
    }
}//GEN-LAST:event_jButtonImprimirActionPerformed

    private void jXComboBoxVehiculosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXComboBoxVehiculosActionPerformed
        try {
            int a = jXComboBoxVehiculos.getSelectedIndex();
            if (a != -1) {
                vehiculos1 = vehiculosController1.getVehiculoslist().get(a);

                jButtonImprimir.setEnabled(true);

            }
        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "Seleccion Combo()");
        }
    }//GEN-LAST:event_jXComboBoxVehiculosActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(ActividadListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(ActividadListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(ActividadListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(ActividadListar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new ActividadListar().setVisible(true);
//            }
//        });
//    }
    private void Imprimir(List<MyCertificacionVehicular> aux, HashMap map) throws JRException {
        try {

            JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(aux);
            //   InputStream reporte = (InputStream) getClass().getResourceAsStream("Actividad.jasper");
            InputStream reporte = (InputStream) getClass().getResourceAsStream("/org/gingresos/reportes/vehiculos/CertificacionVehiculos.jasper");
            if (reporte == null) {
                Mensajeria.Mensaje("No se pudo cargar el reporte");
            } else {

                JasperPrint print = JasperFillManager.fillReport(reporte, map, ds);
                JasperViewer.viewReport(print, false);

            }

        } catch (Exception ex) {
            Mensajeria.MensajeError(ex, "Imprimir()");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.painter.effects.AbstractAreaEffect abstractAreaEffect1;
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonImprimir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private org.jdesktop.swingx.JXComboBox jXComboBoxVehiculos;
    // End of variables declaration//GEN-END:variables
}
